//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "AmericanExercise.h"
using namespace Cephei::QL;
#include <gen/QL/EarlyExercise.h>
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::CAmericanExercise::CAmericanExercise (DateTime latestDate, Microsoft::FSharp::Core::FSharpOption<Boolean>^ payoffAtExpiry) : CEarlyExercise(CAmericanExercise::typeid)
{
    try
    {
#ifdef HANDLE
        _phAmericanExercise = NULL;
#endif
        QuantLib::Date _latestDate = (QuantLib::Date)ValueHelper::Convert (latestDate); //d
        bool _payoffAtExpiry = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (payoffAtExpiry) ? (bool)ValueHelper::Convert (payoffAtExpiry->Value) : false); //4
        _ppAmericanExercise = new boost::shared_ptr<QuantLib::AmericanExercise> (new QuantLib::AmericanExercise ( _latestDate,  _payoffAtExpiry ));
        SetEarlyExercise (boost::dynamic_pointer_cast<QuantLib::EarlyExercise> (*_ppAmericanExercise));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::CAmericanExercise::CAmericanExercise (DateTime earliestDate, DateTime latestDate, Microsoft::FSharp::Core::FSharpOption<Boolean>^ payoffAtExpiry) : CEarlyExercise(CAmericanExercise::typeid)
{
    try
    {
#ifdef HANDLE
        _phAmericanExercise = NULL;
#endif
        QuantLib::Date _earliestDate = (QuantLib::Date)ValueHelper::Convert (earliestDate); //d
        QuantLib::Date _latestDate = (QuantLib::Date)ValueHelper::Convert (latestDate); //d
        bool _payoffAtExpiry = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (payoffAtExpiry) ? (bool)ValueHelper::Convert (payoffAtExpiry->Value) : false); //4
        _ppAmericanExercise = new boost::shared_ptr<QuantLib::AmericanExercise> (new QuantLib::AmericanExercise ( _earliestDate,  _latestDate,  _payoffAtExpiry ));
        SetEarlyExercise (boost::dynamic_pointer_cast<QuantLib::EarlyExercise> (*_ppAmericanExercise));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::CAmericanExercise::CAmericanExercise (boost::shared_ptr<QuantLib::AmericanExercise>& childNative, Object^ owner) : CEarlyExercise(CAmericanExercise::typeid)
{
#ifdef HANDLE
	_phAmericanExercise = NULL;
#endif
	_ppAmericanExercise = &childNative;
    _ppEarlyExercise = new boost::shared_ptr<QuantLib::EarlyExercise> (boost::dynamic_pointer_cast<QuantLib::EarlyExercise> (*_ppAmericanExercise));
}
Cephei::QL::CAmericanExercise::CAmericanExercise (QuantLib::AmericanExercise& childNative, Object^ owner) : CEarlyExercise(CAmericanExercise::typeid)
{
#ifdef HANDLE
	_phAmericanExercise = NULL;
#endif
	_ppAmericanExercise = new boost::shared_ptr<QuantLib::AmericanExercise> (&childNative);
    _ppEarlyExercise = new boost::shared_ptr<QuantLib::EarlyExercise> (boost::dynamic_pointer_cast<QuantLib::EarlyExercise> (*_ppAmericanExercise));
    _AmericanExerciseOwner = owner;
    _EarlyExerciseOwner = owner;
}

Cephei::QL::CAmericanExercise::CAmericanExercise (CAmericanExercise^ copy) : CEarlyExercise(CAmericanExercise::typeid)
{
#ifdef HANDLE
	_phAmericanExercise = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppAmericanExercise = new boost::shared_ptr<QuantLib::AmericanExercise> (copy->GetShared());
        _ppEarlyExercise = new boost::shared_ptr<QuantLib::EarlyExercise> (boost::dynamic_pointer_cast<QuantLib::EarlyExercise> (*_ppAmericanExercise));
    }
}
Cephei::QL::CAmericanExercise::CAmericanExercise (PLATFORM::Type^ t) : CEarlyExercise(CAmericanExercise::typeid)
{
#ifdef HANDLE
	_phAmericanExercise = NULL;
#endif
	if (!t->IsSubclassOf(CAmericanExercise::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::CAmericanExercise::CAmericanExercise (QuantLib::Handle<QuantLib::AmericanExercise>& childNative, Object^ owner)  : CEarlyExercise(CAmericanExercise::typeid)
{
	_phAmericanExercise = &childNative;
	_ppAmericanExercise = &static_cast<boost::shared_ptr<QuantLib::AmericanExercise>>(childNative.currentLink());
    _ppEarlyExercise = new boost::shared_ptr<QuantLib::EarlyExercise> (boost::dynamic_pointer_cast<QuantLib::EarlyExercise> (*_ppAmericanExercise));
    _AmericanExerciseOwner = owner;
}
Cephei::QL::CAmericanExercise::CAmericanExercise (QuantLib::Handle<QuantLib::AmericanExercise> childNative)  : CEarlyExercise(CAmericanExercise::typeid)
{
	_phAmericanExercise = &childNative;
	_ppAmericanExercise = &static_cast<boost::shared_ptr<QuantLib::AmericanExercise>>(childNative.currentLink());
    _ppEarlyExercise = new boost::shared_ptr<QuantLib::EarlyExercise> (boost::dynamic_pointer_cast<QuantLib::EarlyExercise> (*_ppAmericanExercise));
}
#endif
#ifdef STRUCT
Cephei::QL::CAmericanExercise::CAmericanExercise (QuantLib::AmericanExercise childNative)  : CEarlyExercise(CAmericanExercise::typeid)
{
#ifdef HANDLE
	_phAmericanExercise = NULL;
#endif
	_ppAmericanExercise = new boost::shared_ptr<QuantLib::AmericanExercise> (new QuantLib::AmericanExercise (childNative));
    _ppEarlyExercise = new boost::shared_ptr<QuantLib::EarlyExercise> (boost::dynamic_pointer_cast<QuantLib::EarlyExercise> (*_ppAmericanExercise));
}
#endif

Cephei::QL::CAmericanExercise::~CAmericanExercise ()
{
    if (_ppAmericanExercise != NULL)
    {
	    delete _ppAmericanExercise;
        _ppAmericanExercise = NULL;
    }
}
Cephei::QL::CAmericanExercise::!CAmericanExercise ()
{
    if (_ppAmericanExercise != NULL)
    {
	    delete _ppAmericanExercise;
    }
}
QuantLib::AmericanExercise& Cephei::QL::CAmericanExercise::GetReference ()
{
    if (_ppAmericanExercise == NULL) throw REFNEW NativeNullException ();
	return **_ppAmericanExercise;
}
boost::shared_ptr<QuantLib::AmericanExercise>& Cephei::QL::CAmericanExercise::GetShared ()
{
    if (_ppAmericanExercise == NULL) throw REFNEW NativeNullException ();
	return *_ppAmericanExercise;
}
QuantLib::AmericanExercise* Cephei::QL::CAmericanExercise::GetPointer ()
{
    if (_ppAmericanExercise == NULL) throw REFNEW NativeNullException ();
	return &**_ppAmericanExercise;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::AmericanExercise>& Cephei::QL::CAmericanExercise::GetHandle ()
{
	if (_phAmericanExercise == NULL)
	{
		_phAmericanExercise = new Handle<QuantLib::AmericanExercise> (*_ppAmericanExercise);
	}
	return *_phAmericanExercise;
}
#endif
bool Cephei::QL::CAmericanExercise::HasNative () 
{
	return (_ppAmericanExercise != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::IAmericanExercise^ Cephei::QL::CAmericanExercise_Factory::Create (DateTime latestDate, Microsoft::FSharp::Core::FSharpOption<Boolean>^ payoffAtExpiry)
{
    return REFNEW CAmericanExercise ( latestDate,  payoffAtExpiry);
}
Cephei::QL::IAmericanExercise^ Cephei::QL::CAmericanExercise_Factory::Create (DateTime earliestDate, DateTime latestDate, Microsoft::FSharp::Core::FSharpOption<Boolean>^ payoffAtExpiry)
{
    return REFNEW CAmericanExercise ( earliestDate,  latestDate,  payoffAtExpiry);
}
